unit uZip;

interface

uses SysUtils, ZipForge;

const
  Password = '4444';
                           
function Load(const ArchiveFileName, AFileName: PChar): PChar; stdcall;
function Save(const ArchiveFileName, AFileName, AText: PChar): Boolean; stdcall;
function FileExists(const ArchiveFileName, AFileName: PChar): Boolean; stdcall;

implementation

var
  Zip: TZipForge;

function IsFile(const AFileName: PChar): boolean;
var
  ArchiveItem: TZFArchiveItem;
begin
  Result := False;
  with Zip do
  if (FindFirst('*.*', ArchiveItem, faAnyFile - faDirectory)) then
  repeat
    if (Trim(ArchiveItem.FileName) = Trim(AFileName)) then
    begin
      Result := True;
      Exit;
    end;
  until (not FindNext(ArchiveItem));
end;

function FileExists(const ArchiveFileName, AFileName: PChar): Boolean;
begin
  Result := False;
  if not SysUtils.FileExists(ArchiveFileName) then Exit;
  Zip.Password := Password;
  Zip.FileName := ArchiveFileName;
  Zip.OpenArchive;
  try
    Result := IsFile(AFileName);
  finally
    Zip.CloseArchive;
  end;
end;

function Save(const ArchiveFileName, AFileName, AText: PChar): Boolean;
begin
  Result := False;
  Zip.Password := Password;
  Zip.FileName := ArchiveFileName;
  Zip.OpenArchive;
  try
    Zip.AddFromString(AFileName, AText);
    Result := True;
  finally
    Zip.CloseArchive;
  end;
end;

function Load(const ArchiveFileName, AFileName: PChar): PChar;
var
  S: string;
begin
  Result := '';
  if not SysUtils.FileExists(ArchiveFileName) then Exit;
  Zip.Password := Password;
  Zip.FileName := ArchiveFileName;
  Zip.OpenArchive;
  try
    S := '';
    if IsFile(AFileName) then
      Zip.ExtractToString(AFileName, S);
    Result := PChar(S);
  finally
    Zip.CloseArchive;
  end;
end;

initialization
  Zip := TZipForge.Create(nil);

finalization
  Zip.Free;

end.
